package fm.dongman.animefm.view.fragment

import android.content.Context
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast

import fm.dongman.animefm.R
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.RankInfoContract
import fm.dongman.animefm.view.adapter.ComicRankInfoAdapter
import fm.dongman.animefm.view.adapter.ComicRecommendAdapter
import fm.dongman.animefm.view.listener.LoadMoreRecyclerOnScrollListener
import fm.dongman.animefm.view.util.ActivityHelper
import fm.dongman.animefm.view.util.SystemHelper
import fm.dongman.animefm.view.util.ToastHelper
import fm.dongman.animefm.view.widget.MultiStateLayout
import fm.dongman.modellib.StaticArea.Injection
import fm.dongman.presenterlib.presenter.RankInfoPresenter

import kotlinx.android.synthetic.main.fragment_details_rank.*

import org.jetbrains.anko.backgroundColor

/**
 * 榜单详情页面
 * Created by Administrator on 2017/10/23.
 */
class RankInfoFragment : BaseFragment(), RankInfoContract.IRankInfoView {
    private lateinit var mPresenter: RankInfoContract.IRankInfoPresenter // 数据控制器
    private var mFlagType: String = FIND_INFO_WEEK_HOT // 类型 flag
    //榜单变量
    private val mComicList: ArrayList<IModel.IComicModel> = ArrayList()
    private lateinit var mComicListAdapter: ComicRankInfoAdapter
    private lateinit var mScrollListener: LoadMoreRecyclerOnScrollListener // recyclerView 的滑动监听器

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val fragment = inflater!!.inflate(R.layout.fragment_details_rank, container, false)
        RankInfoPresenter(this, Injection.provideRankInfoRepository())
        return fragment
    }

    override fun initData() {
        if (arguments != null) {
            mFlagType = arguments.getString(FIND_TYPE_FLAG)
        }
        onStateChanged(MultiStateLayout.LOADING)
    }

    override fun bindData() {
        bindToolbar()
        bindComicList()
    }

    /**
     * 绑定 Toolbar
     *
     */
    private fun bindToolbar() {
        mRankInfoToolbar.setPadding(0, SystemHelper.getStatusBarHeight(activity), 0, 0)
        isFragmentType()
        mRankInfoToolbar.setNavigationOnClickListener {
            ActivityHelper.backActivity(activity)
        }
    }

    /**
     * 判断点击的是哪一项
     */
    private fun isFragmentType() {
        when (mFlagType) {
            FIND_INFO_WEEK_HOT -> {
                mRankInfoToolbar.backgroundColor = ContextCompat.getColor(activity, R.color.rank_week_hot)
                mRankInfoToolbar.title = getString(R.string.str_ranking_weekly_hot)
            }
            FIND_INFO_NET_TOP -> {
                mRankInfoToolbar.backgroundColor = ContextCompat.getColor(activity, R.color.rank_new_top)
                mRankInfoToolbar.title = getString(R.string.str_ranking_new_top)
            }
            FIND_INFO_GUOCHAN -> {
                mRankInfoToolbar.backgroundColor = ContextCompat.getColor(activity, R.color.rank_domestic)
                mRankInfoToolbar.title = getString(R.string.str_ranking_mainland_ranking)
            }
            FIND_INFO_HAREM -> {
                mRankInfoToolbar.backgroundColor = ContextCompat.getColor(activity, R.color.rank_harem)
                mRankInfoToolbar.title = getString(R.string.str_ranking_harem_ranking)
            }
        }
    }

    /**
     * 绑定动漫列表
     */
    private fun bindComicList() {
        //榜单初始化
        mComicListAdapter = ComicRankInfoAdapter(activity, mComicList)
        mRankInfoRecycler.adapter = mComicListAdapter
        mRankInfoRecycler.layoutManager = GridLayoutManager(activity, 3, GridLayoutManager.VERTICAL, false)
        mScrollListener = object : LoadMoreRecyclerOnScrollListener(mRankInfoRecycler.layoutManager) {
            override fun onLoadMore(currentPage: Int) {
                // 加载更多
                mPresenter.getRankInfo(currentPage, mFlagType)
            }
        }
        mRankInfoRecycler.addOnScrollListener(mScrollListener)
        // 获取动漫列表
        getComicList(1)
    }

    /**
     * 获取动漫列表
     * @param page 页数
     */
    private fun getComicList(page: Int) {
        mPresenter.getRankInfo(page, mFlagType)
    }

    override fun setPresenter(presenter: RankInfoContract.IRankInfoPresenter) {
        mPresenter = presenter
    }

    override fun showNoNetwork() {
        onStateChanged(MultiStateLayout.NET_ERROR)
    }

    override fun showRefreshFailed() {
        ToastHelper.getInstance(activity).showError(getString(R.string.str_refresh_failed))
    }

    override fun showRankList(comics: List<IModel.IComicModel>) {
        onStateChanged(MultiStateLayout.SUCCESS)
        mComicList.clear()
        mComicList.addAll(comics)
        mComicListAdapter.notifyDataSetChanged()
    }

    override fun showMoreRankList(comics: List<IModel.IComicModel>) {
        mComicList.addAll(comics)
        mComicListAdapter.notifyDataSetChanged()
    }

    override fun isActive(): Boolean = isAdded

    override fun getViewContext(): Context = activity

    /**
     * 切换状态
     */
    private fun onStateChanged(state: Int) {
        mRankInfoMultiLayout?.changeState(state)
    }

    companion object {
        val FIND_INFO_WEEK_HOT = RankInfoContract.IRankInfoPresenter.TYPE_WEEK_HOT //本周最火
        val FIND_INFO_NET_TOP = RankInfoContract.IRankInfoPresenter.TYPE_XINFAN  //新番TOP
        val FIND_INFO_GUOCHAN = RankInfoContract.IRankInfoPresenter.TYPE_GUOCHAN  //国产排行
        val FIND_INFO_HAREM = RankInfoContract.IRankInfoPresenter.TYPE_HOUGONG  //后宫排行
        val FIND_TYPE_FLAG = "flag"

        /**
         * 接收 flag
         */
        fun getInstance(flag: String): RankInfoFragment {
            val fragment = RankInfoFragment()
            val args = Bundle()
            args.putString(FIND_TYPE_FLAG, flag)
            fragment.arguments = args
            return fragment
        }
    }
}
